package test.YTool;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class SqlMaker {
	Map<String, Object> paramMap;
	String sql;

	public Map<String, Object> getParamMap() {
		return paramMap;
	}

	public void setParamMap(Map<String, Object> paramMap) {
		this.paramMap = paramMap;
	}

	public String getSql() {
		return sql;
	}

	public void setSql(String sql) {
		this.sql = sql;
	}
	
	/**
	 * 拼接 sql语句中 in 语法用及配套的 paramMap
	 * @param tList in 中的多个对象，最好用String 或 Integer 类型
	 * @return 是否成功
	 */
	boolean makeIn(List<?> tList){
		if( 0==tList.size() )
			return false;
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		StringBuffer sql = new StringBuffer("(");
		Iterator<?> it = tList.iterator();
		
		Object id = it.next();
		String tmp = "id"+id;
		sql.append(":");
		sql.append(tmp);
		paramMap.put(tmp, id);
		
		while(it.hasNext())
		{
			sql.append(", :");
			id = it.next();
			tmp = "id"+id;
			sql.append(tmp);
			paramMap.put(tmp, id);
		}
		sql.append(")");
		
		setSql(sql.toString());
		setParamMap(paramMap);
		
		return true;
	}
}
